#include "LayerSkill.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "deprecated/CCDeprecated.h"
#include "ui/UIHelper.h"
#include "base/CCConsole.h"
#include "logic/LLogicSkill.h"

USING_NS_CC;
using namespace ui;


CLayerSkill::CLayerSkill(void)
{
}


CLayerSkill::~CLayerSkill(void)
{
}

bool CLayerSkill::init() {
	// 1. super init first
	if ( !CLayerBase::init() )
	{
		return false;
	}

	m_Skill = CSLoader::createNode("SkillManager.csb");

	//ui::Button* tLoginB = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Pack, 93);
	//tLoginB->addTouchEventListener(this, toucheventselector(CLayerSkill::onLogin));

	addChild(m_Skill);

	return true;
}

void CLayerSkill::onEnter() {
	CLayerBase::onEnter();
	
	m_Close = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Skill, 61);
	m_Close->addTouchEventListener(this, toucheventselector(CLayerSkill::onClose));

	//m_BList = (ui::ListView*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Skill, 32);
	m_BInit = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Skill, 55);
	m_BInit->addTouchEventListener(this, toucheventselector(CLayerSkill::onBInit));

	m_BUp = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Skill, 56);
	m_BUp->addTouchEventListener(this, toucheventselector(CLayerSkill::onBUp));

	m_BDown = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Skill, 57);
	m_BDown->addTouchEventListener(this, toucheventselector(CLayerSkill::onBDown));

	m_BUpgrade = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Skill, 58);
	m_BUpgrade->addTouchEventListener(this, toucheventselector(CLayerSkill::onBUpgrade));

	//m_LList = (ui::ListView*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Skill, 33);
	m_LInit = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Skill, 59);
	m_LInit->addTouchEventListener(this, toucheventselector(CLayerSkill::onLInit));

	m_LUpgrade = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Skill, 60);
	m_LUpgrade->addTouchEventListener(this, toucheventselector(CLayerSkill::onLUpgrade));

	AnimalSkill& tAllSkill = m_LogicData->GetOneself()->GetSkill();
	m_BList = m_UiManager->InitSkillView(CCocosUiHelper::seekNodeByTag(m_Skill, 91), &tAllSkill.m_BattleSkill.AllSkill[SpellAttack_1], SpellAttack_1, BattleSkillCount, m_BListItems);
	m_LList = m_UiManager->InitSkillView(CCocosUiHelper::seekNodeByTag(m_Skill, 94), &tAllSkill.m_LifeSkill.OtherSkill[OtherSkill_zao], OtherSkill_zao, OtherSkillCount, m_LListItems);
}

/*
void CLayerSkill::onExit() {
	CLayerBase::onExit();
}

void CLayerSkill::update(float delta) {
	CLayerBase::update(delta);
}
*/

/*
void CLayerSkill::onOpenPack(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		
		break;
	}
}*/

void CLayerSkill::onClose(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		this->setVisible(false);
		break;
	}
}

void CLayerSkill::onBInit(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		{
			ssize_t tIndex = m_BList->getCurSelectedIndex();
			CLLogicSkill tLogicSkill;
			int32_t tRet = tLogicSkill.InitPersonBSkill(tIndex + SpellAttack_1);
			if (tRet != CommonErrnoSucceed) {
				CCLOG("error|%s|errno|%d", __FUNCTION__, tRet);
			}
		}
		break;
	}
}

void CLayerSkill::onBUp(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:

		break;
	}
}

void CLayerSkill::onBDown(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:

		break;
	}
}

void CLayerSkill::onBUpgrade(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		{
			ssize_t tIndex = m_BList->getCurSelectedIndex();
			CLLogicSkill tLogicSkill;
			int32_t tRet = tLogicSkill.UpgradePersonBSkill(tIndex + SpellAttack_1);
			if (tRet != CommonErrnoSucceed) {
				CCLOG("error|%s|errno|%d", __FUNCTION__, tRet);
			}
		}
		break;
	}
}

void CLayerSkill::onLInit(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:

		break;
	}
}

void CLayerSkill::onLUpgrade(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:

		break;
	}
}
